<?php

/**
 * 
 * @author yuxun
 *
 */
class MatchServerCommand extends CConsoleCommand {

	//  */1 * * * * /usr/local/bin/php /data/web/webdata/oss/crontab.php matchServer >> /home/webuser/tmp/matchServer.log
	/**
	 * (non-PHPdoc)
	 * @see CConsoleCommand::run()
	 * 当当前时间到重置时间时，将未正式生效的匹配跨服的配置发送到游戏端
     * 发送成功，则将记录记入到正式的匹配跨服记录表中，原先的记录标记is_delete为1
     * 然后再删除未正式的的记录
	 */
    public function run($args) {
        set_time_limit (0);
    	echo "............................... \n";
    	echo "匹配跨服自动生效配置定时任务开始 \n";
    	echo "开始时间：".date('Y-m-d H:i:s',time())."\n";
    	
    	//查询重置时间
    	$game = 'mango';
    	$crossList = self::searchInfo('cross');
    	$reset_time = $crossList['reset_time'];
    	$nowTime = strtotime(date('Y-m-d H:i:00',time()));  //当前时间
    	if($reset_time == $nowTime){
    		$modMatchCrossServerV = new ModMatchCrossServerV();
    		$modMatchCrossSerV = new ModMatchCrossSerV();
    		$match_obj = $modMatchCrossServerV->findAllByAttributes(array('game'=>$game));
    		if(!$match_obj){
    			echo "暂无数据 \n";
    			exit;
    		}else{
    			//将正式的匹配跨服is_delete修改为1
    			$modMatchCrossServer = new ModMatchCrossServer();
    			$modMatchCrossServer->updateAll(array('is_delete'=>1),'game =:game and is_delete=:is_delete',array(':game'=>$game,':is_delete'=>0));
    		}

    		$ids = '';
    		$matchCrossData = array();
    		$matchCrossSerData = array();
    		foreach ($match_obj as $key=>$val){
    			$match_id = $val->id;
    			$match_server_obj = $modMatchCrossSerV->findAllByAttributes(array('match_id'=>$match_id));
    			$match_server_ids = '';
    			if(!$match_server_obj){  //若没有关系，则退出循环
    				//直接删除数据
    				$modMatchCrossServerV->deleteByPk($val->id);
    				continue;
    			}
    			foreach ($match_server_obj as $k=>$v){
    				$match_server_ids .= ','.$v->server_id;
    			}
    			$match_server_ids = substr($match_server_ids, 1);
    			echo "match_server_ids == ".$match_server_ids." \n";
    			$send = array();
    			$send['match_server_ids'] = $match_server_ids;
    			$send['node_name'] = $val->node_name;
    			$send['ip'] = $val->ip;
    			$send['port'] = $val->port;
    			//$s_result = self::send($send);
    			$s_result['state'] = true;
    			if($s_result['state']){  //发送成功
    				$ids .= ','.$val->id;
    				//将匹配跨服加到正式的匹配跨服表
    				$d = array();
    				$d['id'] = $val->id;
    				$d['game'] = $val->game;
    				$d['cross_server_name'] = $val->cross_server_name;
    				$d['node_name'] = $val->node_name;
    				$d['ip'] = $val->ip;
    				$d['port'] = $val->port;
    				$d['create_user'] = 1;
    				$d['create_time'] = time();
    				$d['update_user'] = 1;
    				$d['update_time'] = time();
    				array_push($matchCrossData, $d);
    		
    				foreach ($match_server_obj as $k=>$v){
    					$d = array();
    					$d['match_id'] = $v['match_id'];
    					$d['server_id'] = $v['server_id'];
    					array_push($matchCrossSerData, $d);
    				}
    		
    			}else{
    				echo $val->cross_server_name."发送失败 \n";
    			}
    		}
    		
    		$modMatchCrossServer = new ModMatchCrossServer();
    		$modMatchCrossServer->addmuch($matchCrossData);
    		$modMatchCrossSer = new ModMatchCrossSer();
    		$modMatchCrossSer->addmuch($matchCrossSerData);
    		
    		//删除发送成功的匹配跨服数据（未生效表）
    		$ids = substr($ids, 1);
    		$modMatchCrossServerV->deleteForIds($ids);
    		$modMatchCrossSerV->deleteForMatchIds($ids);
    		echo "匹配跨服配置自动生效成功 \n";
    	}else{
    		echo "暂未到重置时间 \n";
    	}
    	
    	echo "结束时间：".date('Y-m-d H:i:s',time())."\n";
    	echo "匹配跨服自动生效配置定时任务结束 \n";
    }
    
    
    public static function searchInfo($type){
    	$modSystem = new ModSystem();
    	$game = PubBase::getCurrentGame();
    	$params = array();
    	$params['g'] = $game;
    	$params['inc_type'] = $type;
    	$list = $modSystem->select($params);
    	$data = array();
    	foreach ($list as $key=>$val){
    		$data[$val['name']] = $val['value'];
    	}
    	return $data;
    }

   

        
}